package com.bjpowernode.rbac.web.controller;

import com.bjpowernode.rbac.entity.Department;
import com.bjpowernode.rbac.entity.Employee;
import com.bjpowernode.rbac.entity.Permission;
import com.bjpowernode.rbac.entity.Role;
import com.bjpowernode.rbac.query.EmployeeQuery;
import com.bjpowernode.rbac.query.QueryObject;
import com.bjpowernode.rbac.service.PermissionService;
import com.bjpowernode.rbac.service.RoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author jy
 * @version 1.0
 * @date 2022/2/9 16:05
 */
@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private PermissionService permissionService;

    @RequestMapping("/list")
    public String list(Model model,@ModelAttribute("qo") QueryObject queryObject){
        //调用业务层获取数据
        PageInfo<Role> pageInfo = roleService.selectDepartmentByCondition(queryObject);
        //传到页面
        model.addAttribute("pageInfo",pageInfo);
        //跳转到页面
        return "role/list";
    }

    @RequestMapping("/input")
    public String input(Long id,Model model){
        /**
         * id不为空即是编辑，则需要查询数据库回显表单
         */
        if (id != null){
            Role role = roleService.selectRoleById(id);
            model.addAttribute("role",role);
        }
        //否则为添加,添加页面里面又需要把部门全部查出来
        QueryObject queryObject = new QueryObject();
        queryObject.setPageSize(0);
        PageInfo<Permission> permissionPageInfo = permissionService.selectPermissionByCondition(queryObject);
        model.addAttribute("permissions",permissionPageInfo.getList());
        return "role/input";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Role role,Long[] permissionIds){
        roleService.saveOrUpdate(role,permissionIds);
        return "redirect:/role/list";
    }

    @RequestMapping("/delete")
    public String delete(Long id){
        roleService.delete(id);
        return "redirect:/role/list";
    }
}
